import { useState, useEffect } from 'react';
import { terminalService, CommandResult } from '../services/TerminalService';

export const useTerminal = () => {
  const [commandHistory, setCommandHistory] = useState<string[]>([]);
  const [currentOutput, setCurrentOutput] = useState<CommandResult | null>(null);
  const [isLoading, setIsLoading] = useState(false);

  useEffect(() => {
    const subscription = terminalService.commandOutput$.subscribe(
      output => setCurrentOutput(output)
    );
    return () => subscription.unsubscribe();
  }, []);

  const executeCommand = async (command: string) => {
    setIsLoading(true);
    try {
      const result = await terminalService.executeCommand(command);
      setCommandHistory(prev => [...prev, command]);
      return result;
    } finally {
      setIsLoading(false);
    }
  };

  const getCurrentDirectory = () => {
    return terminalService.getCurrentDirectory();
  };

  return {
    executeCommand,
    commandHistory,
    currentOutput,
    isLoading,
    getCurrentDirectory,
  };
}; 